﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Battleship
{
    /**
     * Class
     */
    class WindInfluence
    {
        // Diện tích tâm chiều gió
        private double WindCenterArea;
        // Áp lực gió tính toán
        private double WindPressure;
        // Moomen nghiêng
        private double HeelingMomen;

        private Dictionary<double, double> WindPressure_Map = new Dictionary<double, double>()
        {
            {0.5, 157}, {1.0, 177}, {1.5, 196}, 
            {2.0, 216}, {2.5, 235}, {3.0, 225},
            {4.0, 265}, {5.0, 284}, {6.0, 304}
        };

        private Dictionary<double, double> BTa1_Map = new Dictionary<double, double>()
        {
            {2.5, 0.4}, {3.0, 0.41}, {0.4, 0.46}, 
            {5.0, 0.6}, {6.0, 0.81}, {7.0, 1.00},
            {8.0, 1.2}, {9.0, 1.28}, {10, 1.3}
        };

        private Dictionary<double, double> ZgBa2_Map = new Dictionary<double, double>()
        {
            {0.15, 0.66}, {0.2, 0.58}, {0.25, 0.46}, 
            {0.3, 0.34}, {0.35, 0.22}, {0.5, 0.1},
            {0.45, 0}
        };

        public WindInfluence(double waterVol, double averageDraft)
        {
            HydrostaticInterpolate HI = new HydrostaticInterpolate();
            this.WindCenterArea = HI.hydroInterpolate(waterVol, Constants.COL_ST);

            double ZT = HI.hydroInterpolate(waterVol, Constants.COL_ZT);

            if (ZT <= Constants.ZTMin)
            {
                this.WindPressure = WindPressure_Map[Constants.ZTMin];
            }
            else if (ZT >= Constants.ZTMax)
            {
                this.WindPressure = WindPressure_Map[Constants.ZTMax];
            }
            else
            {
                this.WindPressure = Formulas.linearInterpolation(ZT, WindPressure_Map.Keys.ToArray<double>()
                    , WindPressure_Map.Values.ToArray<double>());
            }

            double a1 = 0.0;
            double BT = Constants.B / averageDraft;
            if (BT <= Constants.BTMin)
            {
                a1 = BTa1_Map[Constants.BTMin];
            }
            else if (BT >= Constants.BTMax)
            {
                a1 = BTa1_Map[Constants.BTMax];
            }
            else
            {
                a1 = Formulas.linearInterpolation(BT, BTa1_Map.Keys.ToArray<double>(),
                    BTa1_Map.Values.ToArray<double>());
            }

            double a2 = 0.0;
            double ZgB = Constants.Zg / Constants.B;
            if (ZgB <= Constants.ZgBMin)
            {
                a2 = ZgBa2_Map[Constants.ZgBMin];
            }
            else if (ZgB >= Constants.ZgBMax)
            {
                a2 = ZgBa2_Map[Constants.ZgBMax];
            }
            else
            {
                a2 = Formulas.linearInterpolation(ZgB, ZgBa2_Map.Keys.ToArray<double>(),
                    ZgBa2_Map.Values.ToArray<double>());
            }

            double d = HI.hydroInterpolate(waterVol, Constants.DI);
            double Z = ZT + a1 * a2 * d;

            this.HeelingMomen = 0.001 * WindPressure * WindCenterArea * Z;
        }
        public double getHeelingMomen()
        {
            return HeelingMomen;
        }
    }
}
